package com.erju.service.impl;

import com.erju.dao.UserMapper;
import com.erju.pojo.User;
import com.erju.service.LoginService;
import com.erju.utils.*;
import lombok.Data;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * @Description: 登录实现类
 * @author: 86132
 * @date: 2022.03.06
 */

@Service
public class LoginServiceImpl implements LoginService {

    private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public Result<?> userNameLogin(User user) throws UnsupportedEncodingException {
        User userR = userMapper.selectByUserName(user.getUsername());
        log.info("要登陆的人" +userR);
        if (userR != null) {
            log.info("----" + JasyptUtil.setPass(user.getPassword()));
            boolean equals = JasyptUtil.getPass(userR.getPassword()).equals(user.getPassword());
            if (equals) {
                String token = JWTUtil.createToken(userR);
                HashMap<Object, Object> hashMap = new HashMap<>();
                hashMap.put("token", token);
                String[] split = userR.getPermissions().split(",");
                log.info(split.toString());
                hashMap.put("permissions", split);
                hashMap.put("user", userR);
                //token存到redis中 过期时间30分钟 用来判断这个token是否是对应id的
                redisUtil.set(token, userR.getUsername(), 60 * 60 * 24);
                return Result.success("登陆成功", hashMap);
            }
        }
        return Result.error("用户名不存在，登陆失败！");
    }

    /**
     * 退出登陆
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    public Result loginOut(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest) request;
        //从请求头中获取token
        String token = req.getHeader("Authorization");
        if (redisUtil.hasKey(token)) {
            redisUtil.del(token);
            return Result.success("退出成功！");
        }
        return Result.error("退出失败！");
    }
    @Autowired
    private BaiduAIUtils baiduAiUtils;

    //扫描人脸登录
    public Result loginByFace(StringBuffer imagebast64) throws JSONException {
        String image = imagebast64.substring(imagebast64.indexOf(",") + 1, imagebast64.length());
        String userId = baiduAiUtils.faceSearch(image);
        return new Result();
    }
    //扫描人脸注册
    public Result registerFace(String userId, StringBuffer imagebast64, HttpServletRequest request) throws JSONException {
        String image = imagebast64.substring(imagebast64.indexOf(",") + 1, imagebast64.length());
        Boolean isSuccess = baiduAiUtils.faceRegister(userId, image);
        if (isSuccess){
            request.getSession().setAttribute("RuserId","1001");
            request.getSession().setAttribute("Rusername","派大星的朋友");
            return new Result();
        }else {
            request.getSession().setAttribute("RuserId","未知");
            request.getSession().setAttribute("Rusername","你不是派大星的朋友");
            return new Result();
        }
    }
}
